package com.everwin.web.utils;

import com.everwin.web.controller.common.service.FileService;
import com.everwin.web.domain.FileDetail;
import com.everwin.web.domain.UploadResultDesc;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;

@Component
public class DownloadPicUtils {
    private static final Logger logger = LoggerFactory.getLogger(DownloadPicUtils.class);

    @Autowired
    private FileStore fileStore;

    @Autowired
    private FileService fileService;

    public String saveFile(InputStream fileStream, String originalFileName) throws IOException, NoSuchAlgorithmException {
        FileDetail fileDetail = new FileDetail();
        logger.info("原始文件名:{}",originalFileName);
        int pointIndex = originalFileName.lastIndexOf(".");
        String fileName = pointIndex<0?originalFileName:originalFileName.substring(0,pointIndex);
        String fileSuffix = pointIndex<0?"":originalFileName.substring(pointIndex+1);
        fileDetail.setFileName(fileName);
        fileDetail.setFileSuffix(fileSuffix);
        File tmp=File.createTempFile(System.currentTimeMillis()+"","psi");
        FileUtils.copyInputStreamToFile(fileStream,tmp);
        String hash = fileStore.put(tmp);
        tmp.delete();
        fileDetail.setFileHash(hash);
        fileService.saveFileDetail(fileDetail);
        return hash;
    }

    public String saveFile(InputStream fileStream, UploadResultDesc resultDesc) throws IOException, NoSuchAlgorithmException {
        String originalFileName = resultDesc.getOriginFileName();
        String hash = saveFile(fileStream,originalFileName);
        resultDesc.setFileHash(hash);
        return hash;
    }

    public UploadResultDesc downloadPicByUrl(String path) {
        UploadResultDesc resultDesc = new UploadResultDesc();
        try {
            URL url=new URL(path);
            HttpURLConnection http=(HttpURLConnection) url.openConnection();
            http.setRequestMethod("GET");
            InputStream is=http.getInputStream();
            String fileOriginName=getFileOriginName(path);
            String extension=getExtension(fileOriginName);
            resultDesc.setOriginFileName(fileOriginName);
            resultDesc.setExtension(extension);
            saveFile(is,resultDesc);
        } catch (Exception e) {
            resultDesc.setSuccess(false);
            logger.warn("DownloadPicUtils.downloadPicByUrl saveFile error",e);
        }
        return resultDesc;
    }

    private String getFileOriginName(String path) {
        String[] strings=path.split("/");
        return strings[strings.length-1];
    }

    private String getExtension(String decodeStr) {
        String extension = decodeStr.substring(decodeStr.lastIndexOf(".") + 1);
        return extension;
    }
}
